<?php include "../public/header.php"?>
<style>
    .shuodiv{padding: 2px 0!important;}
    .shuodiv .shuo{background-color: #F6F6F6;color: #999999;width: 98%;margin: 10px 1% 10px 1%;padding: 10px 0}
    .shuodiv .shuo p{padding: 0 10px}
    .add-kuai-btn{text-align: center;padding: 20px;background-color: #FFFFFF}
    .add-kuai-btn i{margin-right: 5px}
    .change_page .divkuai .tablediv{overflow: visible}
    .change_page .divkuai table .opt_width{width: 100%}
    .short_table_list table .xuhao{min-width: 10px!important;}
</style>
<body class="add_body">
<div class="layui-fluid change_page">
    <form class="layui-form" lay-filter="form-add">
        <div class="divkuai shuodiv" style="">
            <div class="shuo">
                <p>• 审批节点条件为‘或’关系，不支持并行审批，当满足条件就会流程就会进入到对应满足条件的审批节点。</p>
                <p>• 可通过设置条件优先级，实现流程走向的优先级判断；数值越小，优先级越高，当优先级相同时则会随机选择一个条件进行判断。 </p>
                <p>• 当前置节点为人工节点时，不可设置流转条件，条件优先级将影响节点显示顺序（数字越小排序越靠前）。</p>
            </div>
        </div>
        <div class="divkuai">
            <div class="tit"><div class="float-l"><span class="minshu"></span><b>基本信息</b></div><div class="clear"></div></div>
            <div class="layui-form-item layui-form-item-one">
                <label class="layui-form-label"><span class="x-red">*</span>优先级：</label>
                <div class="layui-input-inline opt_width">
                    <select name="level" id="level"></select>
                </div>
            </div>
        </div>

        <div class="kuailistdiv">
<!--            <div class="divkuai short_table_list" key="1">-->
<!--                <div class="tit">-->
<!--                    <div class="float-l"><span class="minshu"></span><b>条件块</b></div>-->
<!--                    <div class="float-r rightopt">-->
<!--                        <a href="javascript:;" class="short_table_add_row"><i class="fa fa-plus"></i></a>-->
<!--                        <a href="javascript:;" class="short_table_up_row"><i class="fa fa-arrow-up"></i></a>-->
<!--                        <a href="javascript:;" class="short_table_down_row"><i class="fa fa-arrow-down"></i></a>-->
<!--                        <a href="javascript:;" class="short_table_copy_row"><i class="fa fa-clipboard"></i></a>-->
<!--                        <a href="javascript:;" class="short_table_del_row colorhong"><i class="fa fa-trash"></i></a>-->
<!--                        <a href="javascript:;" class="short_table_del_row colorhong"><i class="fa fa-remove"></i></a>-->
<!--                    </div><div class="clear"></div>-->
<!--                </div><div class="xian"></div>-->
<!--                <div class="tablediv">-->
<!--                    <table class="layui-table">-->
<!--                        <thead>-->
<!--                        <tr>-->
<!--                            <th class="xuhao">序号</th>-->
<!--                            <th><span class="x-red">*</span>条件字段</th>-->
<!--                            <th><span class="x-red">*</span>条件类型</th>-->
<!--                            <th>条件值</th>-->
<!--                        </tr>-->
<!--                        </thead>-->
<!--                        <tbody class="tbody_list"></tbody>-->
<!--                    </table>-->
<!--                </div>-->
<!--            </div>-->
        </div>

        <div class="add-kuai-btn">
            <button id="addkuai" class="layui-btn layui-btn-self-hui" type="button"><i class="fa fa-plus"></i>添加条件快</button>
        </div>
    </form>
</div>
<?php include "../public/botsub.php" ?>
<script>


    var param_sectionnum = getQueryVariable('sectionnum');
    var param_classid = getQueryVariable('classid');

    var factorArr=[],conderArr=[],kuainum=1;
    layui.use(['form'],function() {
        var form = layui.form;

        var level = 1;
        var list = [];
        var param_domvalue = $('section[key="'+param_sectionnum+'"] .nonedata', parent.document).text() || '';
        if(param_domvalue){
            var valueObj = JSON.parse(param_domvalue);
            level = valueObj.level;
            list = valueObj.list;
        }

        ajaxDataGet(ask_flow_course_assist,{classid: param_classid}).then(function(e){
            var data = e.data;
            var factorArr = e.data.factor;
            var conderArr = e.data.conder;

            let levelArr = e.data.level;
            var level_str = '';
            for(var key in levelArr){
                if(level == levelArr[key]){
                    level_str += '<option value="'+levelArr[key]+'" selected>'+levelArr[key]+'</option>';
                }else{
                    level_str += '<option value="'+levelArr[key]+'">'+levelArr[key]+'</option>';
                }
            }
            $('#level').html(level_str);

            if(list.length < 1){
                str_addkuai(factorArr, conderArr, []);
            }else{
                for (var key in list){
                    str_addkuai(factorArr, conderArr, list[key]);
                }
            }



            $("#addkuai").show();
            $("#addkuai").click(function () {
                str_addkuai(factorArr, conderArr, []);
            });

            render_from();
        });


        form.on('submit(post_sub)', function(data){
            var data = form.val("form-add");
            var levelname = '优先级'+data.level;

            var valuename = ''
            var list = [];
            var listError = '';
            $('.tbody_list').each(function(){
                var __list = [];
                var __valuename = '';
                $(this).children('tr').each(function() {
                    var fieldname = $(this).find('.fieldname').val();
                    var fieldnameText = $(this).find('.fieldname>option:selected').text();
                    var fieldtype = $(this).find('.fieldtype').val();
                    var fieldtypeText = $(this).find('.fieldtype>option:selected').text();
                    var fieldvalue = $(this).find('.fieldvalue').val();
                    if(!fieldname && !fieldtype){
                        return true;
                    }else{
                        if(!fieldname){
                            listError = "请选择条件字段";return false;
                        }
                        if(!fieldtype){
                            listError = "请选择条件类型";return false;
                        }

                        if(__valuename){
                            __valuename += '，'+fieldnameText+' '+fieldtypeText+' '+fieldvalue
                        }else{
                            __valuename += fieldnameText+' '+fieldtypeText+' '+fieldvalue
                        }

                        __list.push({
                            fieldname: fieldname,
                            fieldtype: fieldtype,
                            fieldvalue: fieldvalue,
                        });
                    }
                })
                if(__list.length > 0){
                    list.push(__list);
                }
                if(valuename){
                    valuename += '<br/>或<br/>'+__valuename
                }else{
                    valuename += __valuename
                }
            });
            if(listError){
                layMsgFail(listError);return false;
            }

            data.list = list;
            var datastr = JSON.stringify(data);
            $('section[key="'+param_sectionnum+'"] .levelname', parent.document).text(levelname)
            $('section[key="'+param_sectionnum+'"] .valuename', parent.document).html(valuename)
            $('section[key="'+param_sectionnum+'"] .nonedata', parent.document).text(datastr)
            layer_close();
            return false;
        });
    });


    function str_addkuai(factorArr, conderArr, mingxi) {
        var str = '';
        str += '<div class="divkuai short_table_list" key="'+kuainum+'">';
        str += '<div class="tit">';
        str += '<div class="float-l"><span class="minshu"></span><b>条件块</b></div>';
        str += '<div class="float-r rightopt">';
        str += '<a href="javascript:;" class="short_table_add_row mingxibtn"><i class="fa fa-plus"></i></a>';
        str += '<a href="javascript:;" class="short_table_up_row mingxibtn"><i class="fa fa-arrow-up"></i></a>';
        str += '<a href="javascript:;" class="short_table_down_row mingxibtn"><i class="fa fa-arrow-down"></i></a>';
        str += '<a href="javascript:;" class="short_table_copy_row mingxibtn"><i class="fa fa-clipboard"></i></a>';
        str += '<a href="javascript:;" class="short_table_del_row colorhong"><i class="fa fa-trash"></i></a>';
        str += '<a href="javascript:;" onclick="del_kuai(this)"><i class="fa fa-remove"></i></a>';
        str += '</div><div class="clear"></div>';
        str += '</div><div class="xian"></div>';
        str += '<div class="tablediv">';
        str += '<table class="layui-table">';
        str += '<thead>';
        str += '<tr>';
        str += '<th class="xuhao">序号</th>';
        str += '<th><span class="x-red">*</span>条件字段</th>';
        str += '<th><span class="x-red">*</span>条件类型</th>';
        str += '<th>条件值</th>';
        str += '</tr>';
        str += '</thead>';
        str += '<tbody class="tbody_list">';
        for (var ii in mingxi){
            var xuhao = parseInt(ii)+1;
            str += '<tr>';
            str += '<td class="xuhao">'+xuhao+'</td>';
            str += '<td>';
            str += '<div class="layui-input-inline opt_width">';
            str += '<select class="fieldname">';
            str += '<option value="">请选择</option>';
            for(var key in factorArr){
                if(factorArr[key].name == mingxi[ii].fieldname){
                    str += '<option value="'+factorArr[key].name+'" selected>'+factorArr[key].title+'</option>';
                }else{
                    str += '<option value="'+factorArr[key].name+'">'+factorArr[key].title+'</option>';
                }
            }
            str += '</select>';
            str += '</td>';
            str += '<td>';
            str += '<div class="layui-input-inline opt_width">';
            str += '<select class="fieldtype">';
            str += '<option value="">请选择</option>';
            for(var key in conderArr){
                if(key == mingxi[ii].fieldtype){
                    str += '<option value="'+key+'" selected>'+conderArr[key]+'</option>';
                }else{
                    str += '<option value="'+key+'">'+conderArr[key]+'</option>';
                }
            }
            str += '</select>';
            str += '</td>';
            str += '<td><input class="layui-input fieldvalue" value="'+mingxi[ii].fieldvalue+'"></td>';
            str += '</tr>';
        }
        str += '</tbody>';
        str += '</table>';
        str += '</div>';
        str += '</div>';
        $(".kuailistdiv").append(str);
        str_tablist(kuainum, factorArr, conderArr);
        kuainum++;
    }


    function str_tablist(kuainum, factorArr, conderArr) {
        var str = '';
        str += '<tr>';
        str += '<td class="xuhao">1</td>';
        str += '<td>';
        str += '<div class="layui-input-inline opt_width">';
        str += '<select class="fieldname">';
        str += '<option value="">请选择</option>';
        for(var key in factorArr){
            str += '<option value="'+factorArr[key].name+'">'+factorArr[key].title+'</option>';
        }
        str += '</select>';
        str += '</td>';
        str += '<td>';
        str += '<div class="layui-input-inline opt_width">';
        str += '<select class="fieldtype">';
        str += '<option value="">请选择</option>';
        for(var key in conderArr){
            str += '<option value="'+key+'">'+conderArr[key]+'</option>';
        }
        str += '</select>';
        str += '</td>';
        str += '<td><input class="layui-input fieldvalue"></td>';
        str += '</tr>';
        short_table_opt_func(str,kuainum);
    }


    function del_kuai(obj) {
        $(obj).parents('.divkuai').remove();
    }



</script>
</body>
</html>
